// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <optional>
#include <pollux/functions/sparksql/tests/SparkFunctionBaseTest.h>

using namespace kumo::pollux;
using namespace kumo::pollux::test;
using namespace kumo::pollux::functions::test;

namespace kumo::pollux::functions::sparksql::test {
namespace {

class ArrayGetTest : public SparkFunctionBaseTest {
 protected:
  template <typename T>
  std::optional<T> arrayGet(
      const ArrayVectorPtr& array_vector,
      const std::optional<int32_t>& index) {
    auto input =
        make_row_vector({array_vector, make_constant(index, array_vector->size())});
    return evaluateOnce<T>("get(c0, c1)", input);
  }
};

TEST_F(ArrayGetTest, basic) {
  auto array_vector = make_nullable_array_vector<int32_t>({{1, 2, std::nullopt}});
  EXPECT_EQ(arrayGet<int32_t>(array_vector, -1), std::nullopt);
  EXPECT_EQ(arrayGet<int32_t>(array_vector, 0), 1);
  EXPECT_EQ(arrayGet<int32_t>(array_vector, 1), 2);
  EXPECT_EQ(arrayGet<int32_t>(array_vector, 2), std::nullopt);
  EXPECT_EQ(arrayGet<int32_t>(array_vector, 3), std::nullopt);
  EXPECT_EQ(arrayGet<int32_t>(array_vector, std::nullopt), std::nullopt);
}
} // namespace
} // namespace kumo::pollux::functions::sparksql::test
